package see.battle.object;

import see.battle.Const;
import see.battle.drawer.Drawer;

public class Bullet implements MovingGameObject {
  
  private Drawer _drawer;
  private int _objectId;
  private float _focus;
  private float _position;
  private Background _bg;
  private boolean _exists;
  private long _lastUpdateTime;

  /**
   * @param drawer
   * @param bulletType
   * @param focus
   * @param bg
   * @param creationTime
   */
  public Bullet(Drawer drawer, int bulletType, float focus,
      Background bg, long creationTime, long currentTime) {
    _drawer = drawer;
    if (_drawer != null) _objectId = drawer.getBulletId(bulletType);
    _focus = focus;
    _bg = bg;
    _exists = true;
    _lastUpdateTime = creationTime;
    _position = 1;
    update(currentTime);
  }

  public boolean is_exists() {
    return _exists;
  }

  public float get_position() {
    return _position;
  }

  //@Override
  public void draw() {
    if (_exists) {
      _drawer.draw(_objectId, 
          (_focus + _bg.get_position() - Const.bulletWidth / 2), _position);
    }
  }

  //@Override
  public void update(long currentTime) {
    if (_exists) {
      long steps = (currentTime - _lastUpdateTime) / 
      Const.updateDelay;
      _position -= Const.bulletSpeed * steps;
      _lastUpdateTime += Const.updateDelay * steps;
      if (_position < Const.backgroundSeaLevel) _exists = false; 
    }
  }

}